/*
	Figure 3. Main Effects of Candidate Race on Inferred Ideology and Group Favoritism
	Corresponds to Table S4 in Appendix
	
	Figure 4. Interacted results
	Corresponds to Table S5 in Appendix.
	
*/

use data_study_2.dta, clear


// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[63, 8, 13, 11, 5]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
		
		loc lab = proper("`r'")
		if "`lab'" == "Hispa" loc lab = "Hispanic"
	
		label var `r'_aa0 "`lab' X No Position"
		label var `r'_aa1 "`lab' X Expand"
		label var `r'_aa2 "`lab' X Keep"
		label var `r'_aa3 "`lab' X End"
		
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	
	}
	// set reference policy
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
		
//============================================================================== Regress the things


	// All attributes, pooled 
	eststo clear
	
	reg out_ideo `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden', vce(cluster r_id)
		eststo pool_ideo
		loc b_pool_ideo = string(round(_b[_cons], 0.001), "%9.3f")
		outreg2 using table_s4_fig3results.xls, replace ctitle(Ideological Liberalness)
		
	reg out_fair_bwdiff `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden', vce(cluster r_id)
		eststo pool_fair
		loc b_pool_fair = string(round(_b[_cons], 0.001), "%9.3f")
		outreg2 using table_s4_fig3results.xls, append ctitle(Group Favoritism)
		
	coefplot (pool_ideo, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(a) Ideological Liberalness}") || ///
			(pool_fair, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
			|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ///
			ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) nokey ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		byopts(row(1) note("`notes'", size(vsmall)) t1title("{bf:`title'}", size(small))) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
		headings(cand_black = "{bf: Race}" ///
				cand_female = "{bf: Gender}" ///
				cand_exp_teach = "{bf: Occupation}" ///
				cand_biden_p59 = "{bf: District Vote for Biden}" ///
				cand_dist1 = "{bf: District Racial % [W,B,A,H,O]}" ///
				cand_policy_abort1 = "{bf: Abortion}" ///
				cand_policy_tax1 = "{bf: Tax Policy}" ///
				cand_policy_health1 = "{bf: Healthcare}" ///
				cand_policy_eco1 = "{bf: Energy}" ///
				cand_policy_aa1 = "{bf: Affirmative Action}" ///
				, labsize(vsmall)) 

		addplot 1: ,note("Mean, Baseline Categories = `b_pool_ideo'", size(vsmall)) norescaling
		addplot 2: ,note("Mean, Baseline Categories = `b_pool_fair'", size(vsmall)) norescaling

		graph display, xsize(5) ysize(3.7) margins(vsmall)	
		graph export figure_3.png, as(png) replace
	
	// Effect of candidate attributes on main outcomes, interacted
	reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age, vce(cluster r_id)	
		eststo ideo
		loc b_ideo = string(round(_b[_cons], 0.001), "%9.3f")
		outreg2 using table_s5_fig4results.xls, replace ctitle(Ideological Liberalness)
	
	reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age , vce(cluster r_id)
		eststo fair_bwdiff
		loc b_fair = string(round(_b[_cons], 0.001), "%9.3f")
		outreg2 using table_s5_fig4results.xls, append ctitle(Group Favoritism)
	
	coefplot (ideo, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(a) Ideological Liberalness}") || ///
			(fair_bwdiff, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
			|| , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden' `reg_distpop') ///
			omitted baselevels ms(c) msize(med) ///
			ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) nokey ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		byopts(row(1) t1title("{bf:`title'}", size(small))) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
		headings(white_aa0 = "{bf: White X Affirmative Action}" ///
				black_aa0 = "{bf: Black X Affirmative Action}" ///
				asian_aa0 = "{bf: Asian X Affirmative Action}" ///
				hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
				, labsize(vsmall)) 

		addplot 1: ,note("Mean, Baseline Categories = `b_ideo'", size(vsmall)) norescaling
		addplot 2: ,note("Mean, Baseline Categories = `b_fair'", size(vsmall)) norescaling
	
		graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
		graph export figure_4.png, as(png) replace

		
	

		